////////////////////////////////////////////////////旅游足迹目录////////////////////////////////////
const config = require('../config');

module.exports = function (app, query, jwt) {

  //获取目录集
  app.get('/footprintsrclist', jwt({ secret: config.secret }), function (req, res, next) {
    console.log(req.user.userId);
    const { userId } = req.user;
    let sql = 'select * from footprintsrc where user_id=?';
    let params = [userId]
    console.log(params);
    console.log(sql);
    query(sql, params, function (error, rows, field) {
      if (error) {
        const json = {
          code: false,
          message: error.message
        };
      } else {
        let data = [];
        for (row of rows) {
          data.push({
            id: row.id,   //id
            label: row.name    //目录名称
          });
        }

        console.log(rows)
        const json = {
          code: true,
          message: "获取成功",
          data
        };
        console.log(json);
        res.json(json);
      }
    });
  });

  //新增目录
  app.post('/footprintsrc', jwt({ secret: config.secret }), function (req, res, next) {
    console.log(req.body);
	const footprintsrc = {
      name: req.body.name,
      user_id: req.user.userId,
      create_by: req.user.username,
      create_time: new Date(),
      update_by: req.user.username,
      update_time: new Date()
    };
    query('insert into footprintsrc set ?', footprintsrc, function (error, data, field) {
      if (error) {
        const json = {
          code: false,
          message: error.message
        };
        console.log(json);
        res.json(json);
      } else {
        const json = {
          code: true,
          message: "新增成功"
        };
        console.log(json);
        res.json(json);
      }
    });
  });

  //修改目录
  app.put('/footprintsrc', jwt({ secret: config.secret }), function (req, res, next) {
    const params = [
      req.body.name,
      req.user.userId,
      req.user.username,
      new Date(),
      req.body.id
    ];

    query('update footprintsrc set name=?,user_id=?,update_by=?,update_time=? where id=?', params, function (error, data, field) {
      if (error) {
        const json = {
          code: false,
          message: error.message
        };
        console.log(json);
        res.json(json);
      } else {
        const json = {
          code: true,
          message: "修改成功"
        };
        console.log(json);
        res.json(json);
      }
    })
  });

  //删除目录，并删除目录下的文章
  app.delete('/footprintsrc', jwt({ secret: config.secret }), function (req, res, next) {
    console.log(req.query.id);
    query('delete from footprintsrc where id=?', req.query.id, function (error, data, field) {
      if (error) {
        const json = {
          code: false,
          message: error.message
        };
        console.log(json);
        res.json(json);
      } else {
        query('delete from footprint where footprintsrc_id=?', req.query.id, function (error, data, field) {
          if (error) {
            const json = {
              code: false,
              message: error.message
            };
            console.log(json);
            res.json(json);
          } else {
            const json = {
              code: true,
              message: "删除成功"
            };
            console.log(json);
            res.json(json);
          }
        })
      }
    })
  });
};

